<template>
	<view v-if="goods_info.goods_name" class="goods-big-box">
		<!-- 商品轮播图 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
			<swiper-item v-for="(item,i) in goods_info.pics" :key="i">
				<image :src="item.pics_big" mode="aspectFit" @click="preview(i)"></image>
			</swiper-item>
		</swiper>
		
		<!-- 商品主体区 -->
		<view class="goods-info">
			<!-- 价格 -->
			<view class="goods-price">￥ {{goods_info.goods_price}}</view>
			<!-- 商品介绍 -->
			<view class="goods-info-box">
				<!-- 描述 -->
				<view class="goods-name">{{goods_info.goods_name}}</view>
				<!-- 商品收藏 -->
				<view class="goods-right-box">
					<uni-icons type="star" size="20" color="gray"></uni-icons>
					<view class="fc">收藏</view>
				</view>
			</view>
			<!-- 邮递 -->
			<view class="fy">快递: 免运费</view>
		</view>
		<!-- 图文 -->
		<rich-text :nodes="goods_info.goods_introduce"></rich-text>
		
		<!-- 底部导航 -->
		<view class="goods-nav">
			<uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
import { mapState, mapMutations,mapGetters } from 'vuex'
	export default {
		computed:{
			...mapState('m_cart',[]),
			...mapGetters('m_cart',['total'])
		},
		watch:{
			// total(newVal){
			// 	const findResult = this.options.find(x => x.text === '购物车')
			// 	if(findResult){
			// 		findResult.info = newVal
			// 	}
			// }
			total:{
				handler(newVal){
					const findResult = this.options.find(x => x.text === '购物车')
					if(findResult){
						findResult.info = newVal
					}
				},
				immediate:true
			}
		},
		data() {
			return {
				// 获取商品对象
				goods_info:{},
				options: [{
					icon: 'shop',
					text: '店铺',
				}, {
					icon: 'cart',
					text: '购物车',
					info: 0
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
						color: '#fff'
					}
				],
			}
		},
		onLoad(options) {
			const goods_id = options.goods_id
			this.getGoods(goods_id)
		},
		methods: {
			...mapMutations('m_cart',['addToCart']),
			async getGoods(goods_id){
				let {data:res} = await uni.$http.get('/api/public/v1/goods/detail',{goods_id})
				if(res.meta.status !== 200) return
				
				res.message.goods_introduce = res.message.goods_introduce.replace(/<img/g,'<img style="display:block"').replace('.webp','.jpeg')
				this.goods_info = res.message
			},
			preview(i){
				// 调用图片预览的api uni.previewImage
				uni.previewImage({
					// 图片索引
					current:i,
					// 图片的路径
					urls:this.goods_info.pics.map(x => x.pics_big)
				})
			},
			onClick(e){
				if(e.content.text !== '购物车') return
				uni.switchTab({
					url:'/pages/crat/crat'
				})
			},
			buttonClick(e){
				if(e.content.text==='加入购物车'){
					let { goods_id,goods_name,goods_price,goods_samll_logo } = this.goods_info
					const goods ={
						goods_id,
						goods_name,
						goods_price,
						goods_count :1,
						goods_samll_logo,
						goods_state:true
					}
					this.addToCart(goods)
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
swiper{
	height: 750rpx;
	image{
		width: 100%;
		height: 100%;
	}
}
.goods-info{
	padding: 20rpx 0 20rpx 20rpx;
	
	.goods-price{
		color: #c00000;
		font-size: 36rpx;
		font-weight: 600;
	}
	.goods-info-box{
		display: flex;
		justify-content: space-between;
		.goods-name{
			font-size: 30rpx;
			color: #333;
		}
		.goods-right-box{
			width: 250rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			border-left: 2rpx solid #bbb;
			font-size: 30rpx;
			color: #333;
		}
	}
	.fy{
		color: #888;
		font-size: 24rpx;
	}
}
.goods-nav{
	position: fixed;
	bottom: 0;
	left: 0;
	width: 100%;
}
.goods-big-box{
	padding-bottom: 100rpx;
}
</style>
